package websphinx;

import java.util.Vector;

/**
 * used to stored the pointer of matched elements.
 * 
 * all the stored pointers won't be removed until it's recover by a new pointer
 * 
 * @author canoxu
 * 
 */
public class MatchElemPointer {

	private Vector pointers = new Vector();
	private int pos = 0;

	public MatchElemPointer() {
		clear();
	}

	public boolean addPointer(Element ptr) {
		if (pos == pointers.size())
			pointers.add(ptr);
		else
			pointers.set(pos, ptr);
		++pos;

		return true;
	}

	public void clear() {
		pos = 0;
		pointers.clear();
	}

	public int size() {
		return pointers.size();
	}

	public Vector getValue() {
		return pointers;
	}

	/**
	 * get current pointer. if no pointer stored, return null
	 * 
	 * @return
	 */
	public Element getCurPointer() {
		if (pos == 0)
			return null;
		else
			return (Element) pointers.elementAt(pos - 1);

	}

	/**
	 * remove one pointer and return the removed pointer if no pointer stored,
	 * return null
	 * 
	 * @return
	 */
	public Element rollUp() {
		if (pos == 0)
			return null;

		Element elem = (Element) pointers.elementAt(pos - 1);
		--pos;

		return elem;

	}

}
